﻿using System.Web;

namespace Anmlz.Web.Filters
{
    using System.Web.Http.Controllers;
    using System.Web.Http.Filters;
    using System.Web.Mvc;

    using Anmlz.Services.Contracts;

    using ActionFilterAttribute = System.Web.Http.Filters.ActionFilterAttribute;

    public class EnableForceRequestLanguageAttribute : ActionFilterAttribute
    {

        private readonly ILanguageService _languageService;

        public EnableForceRequestLanguageAttribute()
        {
            _languageService = DependencyResolver.Current.GetService<ILanguageService>();
        }

        private const string ForceLanguageHeaderKey = "Force-Language";
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            var forceLanguageHeader = HttpContext.Current.Request.Headers[ForceLanguageHeaderKey];
            if (forceLanguageHeader != null)
            {
                _languageService.ForceRequestLanguage(forceLanguageHeader);
            }
        }

        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
           // _languageService.RemoveForcedRequestLanguage();
        }
    }
}